<template>
  <div class="login">
    <div>
      <div class="bg"></div>
      <back path="/profile" />
      <div class="form">
        <h2>登录</h2>
        <div class="uname">
          <img src="../../assets/img/Profile/user/yonghu.svg" alt="" />
          <div>账号：</div>
          <input type="text" v-model="username" @blur="uname_warn"  />
        </div>
        <div class="warn">
          <p v-show="uwarn">
            {{ this.uwarnMsg }}
          </p>
        </div>
        <div class="psd">
          <img src="../../assets/img/Profile/user/mima.svg" alt="" />
          <div>密码：</div>
          <input type="password" v-model="password" @blur="psd_warn" />
        </div>
        <div class="warn">
          <p v-show="pwarn">
            {{ this.pwarnMsg }}
          </p>
        </div>
        <div class="button" @click="login">登录</div>
        <div class="to-reg" @click="to_reg">去注册</div>
      </div>
    </div>
  </div>
</template>

<script>
import Back from "../../components/Back/Back.vue";

export default {
  components: { Back },
  name: "",
  data() {
    return {
      username: "",
      password: "",
      uwarn: false,
      pwarn: false,
      uwarnMsg: "请输入6-11位的用户名",
      pwarnMsg: "请输入8-11位的密码"
    };
  },
  computed: {
    isReg() {
      return (
        !this.uwarn &&
        !this.pwarn &&
        this.username.length > 0 &&
        this.password.length > 0
      );
    }
  },
  activated(){
    this.$emit('hide')
  },
  deactivated(){
    this.$emit('show')
  },
  methods: {
    to_reg() {
      this.$router.push("/reg");
    },
    uname_warn() {
      let length = this.username.length;
      if (length < 6 || length > 11) {
        this.uwarn = true;
      } else {
        this.uwarn = false;
      }
    },
    psd_warn() {
      let length = this.password.length;
      if (length < 8 || length > 11) {
        this.pwarn = true;
      } else {
        this.pwarn = false;
      }
    },
    login() {
      // console.log(this.isReg);
      if (this.isReg) {
        let user = {
          username: this.username,
          password: this.password
        };
        this.$axios({
          url: "users/login",
          method: "post",
          data: user
        }).then(res => {
          alert(res.data.msg);
          if (!res.data.code) {
            let userInfo = res.data.userInfo
            this.username = "";
            this.password = "";
            this.$store.dispatch('SetToken',res.data.token)
            this.$store.dispatch('SaveUserInfo',res.data.userInfo)
            this.$store.dispatch('ChangeStatus')          
            this.$router.push("/profile");
          }
        });
      } else {
        return alert("请输入合法的用户名与密码");
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
  color: #fff;
  height: 100vh;
  position: relative;
  overflow: hidden;
}
.bg {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  z-index: -1;
  opacity: 0.8;
  background: url("../../assets/img/Profile/user/bg3.jpg");
  background-size: cover;
}
.form {
  width: 100%;
  position: absolute;
  top: 20%;
  left: 0;
  right: 0;
}
h2 {
  text-align: center;
  margin-bottom: 20px;
}
.form img {
  width: 30px;
  height: 30px;
  margin-top: 4px;
  margin-left: 4px;
}
.uname,
.psd {
  display: flex;
  width: 80%;
  height: 40px;
  border: 1px solid #fff;
  border-radius: 16px;
  margin: auto;
  margin-bottom: 5px;
  line-height: 40px;
}
.uname div {
  margin-left: 5px;
}
.psd div {
  margin-left: 5px;
}
input {
  padding-top: 4px;
  background-color: transparent;
  font-size: 14px;
  border: 0px;
  outline: none;
  color: #fff;
}
.warn {
  height: 30px;
  text-align: center;
  color: red;
}
.button {
  width: 60%;
  height: 40px;
  border-radius: 16px;
  line-height: 40px;
  text-align: center;
  background-color: rgb(32, 136, 32);
  margin: auto;
}
.to-reg {
  color: #fff;
  width: 60px;
  margin: 0 auto;
  margin-top: 10px;
  font-size: 18px;
}
</style>
